//
//  NetUtils.swift
//  ddx0513
//
//  Created by ejian  on 15/6/23.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import Foundation

class NetUtils {
    //接口地址
    //static let baseURL = "http://www.ilives.com.cn/educate"
    static let baseURL = "http://www.linhoo.com.cn/educate"
    
    //基本信息
    class func getURLBase() ->String {
        return baseURL + "/other/base.api"
    }
    
    //手机号是否存在
    class func getURLMobileExist() ->String {
        return baseURL + "/user/mobileexist.api"
    }
    
    //发送短信码
    class func getURLSendCode() ->String {
        return baseURL + "/code/sendcode.api"
    }
    
    //验证短信码
    class func getURLValidCode() ->String {
        return baseURL + "/code/validcode.api"
    }
    
    //意见反馈
    class func getURLFeedback() ->String {
        return baseURL + "/other/feedback.api"
    }
    
    //消息推送
//    class func getURL() ->String {    return baseURL + ""
    
    //注册
    class func getURLRegister() ->String {
        return baseURL + "/user/register.api"
    }
    
    //登陆
    class func getURLLogin() ->String {
        return baseURL + "/user/login.api"
    }
    
    //找回密码
    class func getURLResetPwd() ->String {
        return baseURL + "/user/resetpwd.api"
    }
    
    //修改用户信息
    class func getURLModifyInfo() ->String {
        return baseURL + "/user/modifyinfo.api"
    }
    
    //课程首页
    class func getURLLessonList() ->String {
        return baseURL + "/lesson/index.api"
    }
    
    //正在学习
    class func getURLStudying() ->String {
        return baseURL + "/lesson/studying.api"
    }
    
    //已学完
    class func getURLStudied() ->String {
        return baseURL + "/lesson/studied.api"
    }
    
    //课程信息
    class func getURLLessonInfo() ->String {
        return baseURL + "/lesson/lesson.api"
    }
    
    //课程评价
    class func getURLLessoonComments() ->String {
        return baseURL + "/lesson/comments.api"
    }
    
    //播放视频
    class func getURLSectionVideo() ->String {
        return baseURL + "/lesson/sectionvideo.api"
    }
    
    //保存进度
    class func getURLSaveProgress() ->String {
        return baseURL + "/lesson/saveprogress.api"
    }
    
    //提交评价
    class func getURLAddComment() ->String {
        return baseURL + "/lesson/addcomment.api"
    }
    
    //购买会员
    class func getURLBuyVip() ->String {
        return baseURL + "/pay/buyvip.api"
    }
    
    //查询订单状态
    class func getURLQueryOrder() ->String {
        return baseURL + "/pay/queryorder.api"
    }
    
    //短信开通会员
    class func getURLOrderVip() ->String {
        return baseURL + "/pay/ordervip.api"
    }
    
    //短信验证会员
    class func getURLValidVip() ->String {
        return baseURL + "/pay/validvip.html"
    }
    
    //后台接口调用
    class func netRequest(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, view: UIView,
        responseHandler: (() -> Void)?, successHandler: ((JSON) -> Void)?) {
            
            request(method, URLString, parameters: parameters)
                .responseJSON { (_request, _response, data, error) -> Void in
                    if let responseHandler1 = responseHandler {
                        responseHandler1()
                    }
                    
                    if (data == nil) { //网络错误
                        MBHudUtils.showText("网络错误", view: view)
                    } else {
                        var s = JSON(data!)
                        if let retCode = s["retcode"].int {
                            if (retCode == 0) { //接口调用成功
                                if let successHandler1 = successHandler {
                                    successHandler1(s["data"])
                                }
                            } else { //接口调用失败，参见接口协议错误代码表
                                MBHudUtils.showText(s["retmsg"].string!, view: view)
                            }
                        } else { //json格式错误，系统错误
                            MBHudUtils.showText("系统错误", view: view)
                        }
                    }
            }
    }
    
    //后台接口调用
    class func netRequest(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil,
        responseHandler: (() -> Void)?, netErrorHandler: (() -> Void)?, successHandler: ((JSON) -> Void)?,
        interErrorHandler: ((Int, String) -> Void)?, jsonErrorHandler: (() -> Void)?) {
            
        request(method, URLString, parameters: parameters)
        .responseJSON { (_request, _response, data, error) -> Void in
            if let responseHandler1 = responseHandler {
                responseHandler1()
            }
            if (data == nil) { //网络错误
                if let netErrorHandler1 = netErrorHandler {
                    netErrorHandler1()
                }
            } else {
                var s = JSON(data!)
                if let retCode = s["retcode"].int {
                    if (retCode == 0) { //接口调用成功
                        if let successHandler1 = successHandler {
                            successHandler1(s["data"])
                        }
                    } else { //接口调用失败，参见接口协议错误代码表
//                        println(s["retmsg"].string!.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding))
                        if let interErrorHandler1 = interErrorHandler {
                            interErrorHandler1(retCode, s["retmsg"].string!)
                        }
                    }
                } else { //json格式错误，系统错误
                    if let jsonErrorHandler1 = jsonErrorHandler {
                        jsonErrorHandler1()
                    }
                }
            }
        }
    }
    
    //视频接口调用
    class func videoNetRequest(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, view: UIView,
        responseHandler: (() -> Void)?, successHandler: ((JSON) -> Void)?) {
            
            request(method, URLString, parameters: parameters)
                .responseJSON { (_request, _response, data, error) -> Void in
                    
                    if let responseHandler1 = responseHandler {
                        responseHandler1()
                    }
                    
                    if (data == nil) { //网络错误
                        MBHudUtils.showText("网络错误", view: view)
                    } else {
                        var s = JSON(data!)
                        if let retCode = s["error"].string {
                            if (retCode == "0") { //接口调用成功
                                
                                if let successHandler1 = successHandler {
                                    successHandler1(s["data"])
                                }
                            } else { //接口调用失败，参见接口协议错误代码表
                                MBHudUtils.showText("读取视频失败", view: view)
                            }
                        } else { //json格式错误，系统错误
                            MBHudUtils.showText("系统错误", view: view)
                        }
                    }
            }
    }
    
    //视频接口调用
    class func videoNetRequest(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil,
        responseHandler: (() -> Void)?, netErrorHandler: (() -> Void)?, successHandler: ((JSON) -> Void)?,
        interErrorHandler: ((String) -> Void)?, jsonErrorHandler: (() -> Void)?) {
            
            request(method, URLString, parameters: parameters)
                .responseJSON { (_request, _response, data, error) -> Void in
                    
                    if let responseHandler1 = responseHandler {
                        responseHandler1()
                    }
                    
                    if (data == nil) { //网络错误
                        if let netErrorHandler1 = netErrorHandler {
                            netErrorHandler1()
                        }
                    } else {
                        var s = JSON(data!)
                        if let retCode = s["error"].string {
                            if (retCode == "0") { //接口调用成功
                                if let successHandler1 = successHandler {
                                    successHandler1(s["data"])
                                }
                            } else { //接口调用失败，参见接口协议错误代码表
                                if let interErrorHandler1 = interErrorHandler {
                                    interErrorHandler1(retCode)
                                }
                            }
                        } else { //json格式错误，系统错误
                            if let jsonErrorHandler1 = jsonErrorHandler {
                                jsonErrorHandler1()
                            }
                        }
                    }
            }
    }
}